package com.example.todolist;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Date;

public class AddActivity extends AppCompatActivity {


    EditText ddl;
    String choosedate;
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        String UpDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
        ddl=findViewById(R.id.choose_date);
        /*创建点击事件*/
        ddl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calendar calendar=Calendar.getInstance();
                new DatePickerDialog( AddActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        String text = "你选择了：" + year + "年" + (month + 1) + "月" + dayOfMonth + "日";
                        Log.i("text:",text);
                        month=month+1;
                        if(month>9 & dayOfMonth>9) {
                            choosedate = year + "-" +month+"-"+dayOfMonth;
                        }else if(month>9 & dayOfMonth<9){
                            choosedate = year + "-" +month+"-0"+dayOfMonth;
                        }else if(month<9 & dayOfMonth>9){
                            choosedate = year + "-0" +month+"-"+dayOfMonth;
                        }else{
                            choosedate = year + "-0" +month+"-0"+dayOfMonth;
                        }
                        Log.i("choosedate",choosedate);
                        Integer i=choosedate.compareTo(UpDateStr);
                        Log.i("i:",String.valueOf(i));


                        if(i>=0) {
                            Toast.makeText(AddActivity.this, text, Toast.LENGTH_SHORT).show();
                            ddl.setText(choosedate);
                        }else{
                            Toast.makeText(AddActivity.this, "该日期已过去，请重新选择", Toast.LENGTH_SHORT).show();
                        }

                    }
                }
                        ,calendar.get(Calendar.YEAR)
                        ,calendar.get(Calendar.MONTH)
                        ,calendar.get(Calendar.DAY_OF_MONTH)).show();
            }

        });
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void save(View btn){
        TextView title=findViewById(R.id.add_title);
        TextView detail=findViewById(R.id.add_detail);

        String t=title.getText().toString();
        String d=detail.getText().toString();
        String date=ddl.getText().toString();

        Log.i("add","title:"+t);
        Log.i("add","title:"+t);
        DBManager dbManager = new DBManager(AddActivity.this);

        ListItem listItem=new ListItem();
        listItem.setTitle(t);
        listItem.setDetail(d);
        listItem.setDate((new SimpleDateFormat("yyyy-MM-dd")).format(new Date()));
        listItem.setSituation("未完成");
        listItem.setDdl(date);

        dbManager.add(listItem);

        Log.i("add","添加成功");

        startActivity(new Intent(this, MainActivity.class));
        finish(); //你可以考虑 销毁上一个Activity

    }


}